﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MockupDesigner.Items;
using System.Windows.Controls;
using System.Windows.Input;

namespace MockupDesigner.Editors.TextEditors
{
    public abstract class ContentControlContainerEditor : TextEditorBase
    {
        protected ContentControlContainerItem itemToEdit;

        protected abstract string[] GetItemSeparators();
        protected abstract IList<ContentControl> GetContentControlsForItems(string[] items);
        protected abstract string GetStringItems();
        public abstract bool AcceptsReturn { get; }


        public override void element_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            string[] items = base.element.Text.Split(GetItemSeparators(), StringSplitOptions.RemoveEmptyEntries);
            this.itemToEdit.ItemsSource = GetContentControlsForItems(items).ToArray();
        }

        public override void element_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (this.AcceptsReturn)
                return;
            if (e.Key == Key.Enter)
            {
                element_LostFocus(sender, e);
            }
        }

        public override void SetItem(ItemBase itemToEdit)
        {
            base.SetItem(itemToEdit);
            this.itemToEdit = itemToEdit as ContentControlContainerItem;
            base.element.Text = GetStringItems();
            base.element.AcceptsReturn = this.AcceptsReturn;
            if (base.element.Height < 150)
            {
                base.element.Height = 150;
            }
            if (base.element.Width < 150)
            {
                base.element.Width = 150;
            }
        }
    }
}
